unit ucontrolepedido;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActnList, DB, DBTables, StdCtrls, Buttons, DBCtrls, ExtCtrls,
  Grids, DBGrids, ComCtrls;

type
  Tfcontrolepedido = class(TForm)
    StatusBar1: TStatusBar;
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    DBText1: TDBText;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    Label2: TLabel;
    DBText2: TDBText;
    SpeedButton3: TSpeedButton;
    Table1: TTable;
    DataSource1: TDataSource;
    ActionList1: TActionList;
    foco: TAction;
    imprimirA: TAction;
    imprimirT: TAction;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel2: TPanel;
    Label1: TLabel;
    DBGrid2: TDBGrid;
    Panel5: TPanel;
    DBText3: TDBText;
    DBText4: TDBText;
    ajuda: TAction;
    procedure focoExecute(Sender: TObject);
    procedure imprimirAExecute(Sender: TObject);
    procedure imprimirTExecute(Sender: TObject);
    procedure DBText1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure DBGrid2ColExit(Sender: TObject);
    procedure DBGrid2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure DBText4Click(Sender: TObject);
    procedure ajudaExecute(Sender: TObject);
  private

  procedure AppHint(Sender : TObject);

    { Private declarations }
  public
    { Public declarations }
  end;

var
  fcontrolepedido: Tfcontrolepedido;

implementation

uses upedido, UnitRelatorio, UnitMenu, udm, clipbrd;

{$R *.dfm}


procedure Tfcontrolepedido.AppHint(Sender: TObject);
begin
  StatusBar1.Panels[0].Text := Application.Hint;
end;


procedure Tfcontrolepedido.focoExecute(Sender: TObject);
begin
        if dbgrid1.Focused = true then
         dbgrid2.SetFocus
        else
         dbgrid1.SetFocus;

end;

procedure Tfcontrolepedido.imprimirAExecute(Sender: TObject);
var
pd:string;
bookmark : tbookmarkstr;
begin


       bookmark := fpedido.tbpedido.bookmark;

       if  application.MessageBox('Imprimir somente este pedido?','Imprimir pedido (s)',4 + MB_ICONWARNING ) = mryes then begin

         pd := (fpedido.tbpedido['pedido']);

        fpedido.tbpedido.Filter := 'pedido='''+ pd +'''';
         fpedido.tbpedido.filtered := true;
          end;


        Application.CreateForm(tFrPedido, FrPedido);

       fmenu.logotipoExecute(self);
        if fileexists (extractfilepath(paramstr(0)) + '\imagem\logo.bmp') then begin
         FrPedido.imagem5.Picture.assign(clipboard);
          clipboard.clear;
  end;

    if fileexists (extractfilepath(paramstr(0)) + '\imagem\dado.txt') then
     FrPedido.memo5.lines.loadfromfile(extractfilepath(paramstr(0)) + '\imagem\dado.txt');

      FrPedido.quickpedidosaldo.previewmodal;
       FrPedido.Release;
        fpedido.tbpedido.Filtered := false;

          fpedido.tbpedido.Bookmark := bookmark;


end;

procedure Tfcontrolepedido.imprimirTExecute(Sender: TObject);
begin
       with fdm.query1 do begin
        close;
         sql.clear;
          sql.add('select * from tbpedidoitem where saldo > "0" order by pedido');
           open;
            end;

            if fdm.query1['codigo']<> null then begin

        Application.CreateForm(tFrPedido, FrPedido);

       fmenu.logotipoExecute(self);
        if fileexists (extractfilepath(paramstr(0)) + '\imagem\logo.bmp') then begin
         FrPedido.image6.Picture.assign(clipboard);
          clipboard.clear;
  end;

    if fileexists (extractfilepath(paramstr(0)) + '\imagem\dado.txt') then
     FrPedido.memo6.lines.loadfromfile(extractfilepath(paramstr(0)) + '\imagem\dado.txt');

      FrPedido.quickpedidosaldotudo.previewmodal;
       FrPedido.Release;


    end else showmessage('Não há saldo em nenhum pedido');

        fdm.query1.close;

end;

procedure Tfcontrolepedido.DBText1Click(Sender: TObject);
var
st:string;
bok:boolean;
begin
        BOK := inputquery('Localizar Pedido','Entre com o número:',ST);
         if BOK then
          if not fpedido.tbpedido.Locate ('pedido',ST,[]) then
           ShowMessage ('Pedido não encontrado!!!');
            ST := '';


end;

procedure Tfcontrolepedido.SpeedButton2Click(Sender: TObject);
begin
        fpedido.tbpedido.Prior;
end;

procedure Tfcontrolepedido.SpeedButton1Click(Sender: TObject);
begin
        fpedido.tbpedido.Next;
end;

procedure Tfcontrolepedido.SpeedButton3Click(Sender: TObject);
var
st:string;
begin
          st:='A';

        if inputquery ('Opções de impressão','Digite "A" para imprimir todos os ítens' + #13 + 'Digite "T" para imprimir somente saldo:', st) then

          if uppercase(st) ='A' then
           imprimirAexecute(self)
          else  if uppercase(st) ='T' then
           imprimirTexecute(self)
          else
           ShowMessage('Esta opção não é válida');


end;

procedure Tfcontrolepedido.DBGrid2ColExit(Sender: TObject);
var
tot: real;
bookmark : tbookmarkstr;
begin

     bookmark := table1.Bookmark;

     try
      table1.DisableControls;
      tot := 0;

    try
    table1.First;

    while not table1.Eof do begin

    tot := tot + (table1.fieldbyname ('qtd') as tfloatfield).Value;
    table1.next;
    end;
    finally

    table1.EnableControls;
    end;
    finally
    table1.Bookmark := bookmark;
    end;


    if tot <= fpedido.tbitem['qtd'] then begin
     fpedido.tbitem.Edit;
      fpedido.tbitem['qtdentrada'] := tot;
       fpedido.tbitem['saldo'] := (fpedido.tbitem.fieldbyname ('qtd') as tfloatfield).Value -
        (fpedido.tbitem.fieldbyname ('qtdentrada') as tfloatfield).Value;
         fpedido.tbitem.post;

    end else begin

     Showmessage ('A entrada é maoir que o pedido!!!    Verificar!!!');
      table1.Edit;
       table1['qtd'] := null;
        abort;
         end;


end;

procedure Tfcontrolepedido.DBGrid2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = 13 then
         if dbgrid2.SelectedIndex = 0 then
           dbgrid2.SelectedIndex := 1
         else
           dbgrid2.SelectedIndex := 0;


        if key = 27 then
         if dbgrid2.SelectedIndex = 0 then
           dbgrid2.SelectedIndex := 1
         else
           dbgrid2.SelectedIndex := 0;

end;

procedure Tfcontrolepedido.FormActivate(Sender: TObject);
begin
  table1.Open;
 (table1.fieldbyname ('dataentrada') as tdatetimefield).editmask:= '99/99/9999';
 (table1.fieldbyname ('qtd') as tfloatfield).displayformat := '00';
 dbgrid2.SetFocus;
 dbgrid2.SelectedIndex := 0;

end;

procedure Tfcontrolepedido.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
        Application.OnHint := nil; 
        table1.Close;
end;

procedure Tfcontrolepedido.FormCreate(Sender: TObject);
begin
  Application.OnHint := AppHint;
end;

procedure Tfcontrolepedido.DBText4Click(Sender: TObject);
var
st:string;
bok:boolean;
begin
        BOK := inputquery('Localizar Pedido','Entre com o nome do cliente:',ST);
         if BOK then
          if not fpedido.tbpedido.Locate ('fantasia',uppercase(ST),[]) then
           ShowMessage ('Cliente não encontrado!!!');
            ST := '';


end;

procedure Tfcontrolepedido.ajudaExecute(Sender: TObject);
var
  Comp: Trichedit; // * variável Comp da classe TMemo
  texto : string;
begin
  Comp := Trichedit.Create (Self);  // * cria o componente Memo
  Comp.Parent := Self;    // * exibe o componente criado

  if fileexists (extractfilepath(paramstr(0)) + '\ajuda\ajudapedidobaixa.txt') then
   comp.lines.loadfromfile (extractfilepath(paramstr(0)) + '\ajuda\ajudapedidobaixa.txt');

  texto := comp.Text;
  comp.Destroy;

  Showmessage (texto);


end;

end.
